﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using p2p.Misc.Base64;

namespace p2p.File.FileManager
{
    public class FileHandler
    {
        // save the location of the filePath for reading and writing
        private static string FilePath;
        // save the content of the string
        private static string Content;

        public FileHandler(string filePath)
        {
            // specifys the file path that is to be used
            FilePath = filePath;
        }

        private string getContent(){
            // were loading check the file is there
            if (System.IO.File.Exists(FilePath))
            {
                // are we able to read the file
                try
                {
                    // create a file stream to read the file from
                    FileStream fs = new FileStream(FilePath, FileMode.Open);
                    // create a store for the raw file data using the content length as the array
                    byte[] data = new byte[fs.Length];
                    // read the entire file in starting at point 0 and read the full length
                    fs.Read(data, 0, (int)fs.Length);
                    // close the file now we have read it
                    fs.Close();
                    //  return the data an an ASCII string so we can use it
                    return System.Text.Encoding.ASCII.GetString(data);
                }
                catch (Exception)
                {
                    // no throw an exception a developer and understand and find easy
                    throw new FileManagerExceptions.FileReadException();
                }
            }
            else
            {
                // no throw an exception a developer and unserstand and find easy
                throw new FileManagerExceptions.FileNotFoundException();
            }
        }

        public object ReadFile()
        {
            // read the file data in to a string
            string content = getContent();

            // enable the FileSerialization system and provide the XML Data though the base64 dll decoder
            FileSerialize FS = new FileSerialize(Base64.Decode(content));
            return FS.getDycrypt();
        }

        public void WriteFile(FileLayout.p2pfile content, bool overwrite = true)
        {
            // does the file exsist
            if (System.IO.File.Exists(FilePath) && overwrite == false)
            {
                // yes oop's it should not now where writing a new file
                throw new FileManagerExceptions.FileAllreadyExsistsException();
            }
            else
            {
                // start the file system given the object that need to be changed into our Serialization
                FileSerialize FS = new FileSerialize(content);
                // get the encrypted text into a string
                string encryptedContent = FS.getEncrypt();
                // do we have a problem writing to this file
                try
                {
                    // create the file stream
                    FileStream fs = new FileStream(FilePath, FileMode.Create);
                    // create a byte array data from our content using our content lenght 
                    // for the size of the array
                    byte[] data = System.Text.Encoding.ASCII.GetBytes(encryptedContent);
                    // write the data raw into the file
                    fs.Write(data, 0, data.Length);
                    // close the writer
                    fs.Close();
                }
                catch (Exception)
                {
                    // yes throw and error the developer can understand and find easily
                    throw new FileManagerExceptions.FileWriteException();
                }
            }
        }
    }
}
